<template>
    <el-dialog v-model="visible" title="修改膳食日历" width="500px" :close-on-click-modal="false"
        class="edit-meal-schedule-dialog">
        <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" class="form-body">
            <el-form-item label="膳食名" prop="name">
                <el-select v-model="form.name" placeholder="请选择膳食名" style="width: 100%">
                    <el-option v-for="item in mealOptions" :key="item.mealitemId" :label="item.name"
                        :value="item.name" />
                </el-select>
            </el-form-item>

            <el-form-item label="星期">
                <el-input v-model="form.day" disabled />
            </el-form-item>

            <el-form-item label="餐次">
                <el-input v-model="form.meal" disabled />
            </el-form-item>

            <el-form-item label="启用状态" prop="status">
                <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%">
                    <el-option label="启用" value="2" />
                    <el-option label="停用" value="3" />
                </el-select>
            </el-form-item>
        </el-form>

        <template #footer>
            <div class="dialog-footer">
                <el-button @click="visible = false">取消</el-button>
                <el-button type="primary" @click="submit">确认修改</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { ref, defineExpose, defineEmits } from 'vue'
import {inject} from 'vue'
const axios = inject("axios")
import { ElMessage } from 'element-plus'

const visible = ref(false)
const formRef = ref()
const emit = defineEmits(['success'])

const form = ref({
    mpId: '',
    name: '',
    day: '',
    meal: '',
    status: ''
})

const rules = {
    name: [{ required: true, message: '请选择膳食名', trigger: 'change' }],
    status: [{ required: true, message: '请选择启用状态', trigger: 'change' }]
}

const mealOptions = ref([])

const open = async (row) => {
    visible.value = true
    form.value = {
        mpId: row.mealpick.mpId,
        name: row.mealitem.name,
        day: row.mealSchedule.day,
        meal: row.mealSchedule.meal,
        status: row.mealpick.status
    }
    try {
        const res = await axios.post('/mealitem/getMealitem')
        mealOptions.value = res.data
    } catch (err) {
        ElMessage.error('获取菜品失败')
    }
}

const submit = () => {
    formRef.value.validate(async (valid) => {
        if (!valid) {
            ElMessage.warning('请填写完整信息')
            return
        }

        try {
            const res = await axios.post('/mealSchedule/updateMealSchedule', null, {
                params: {
                    mpId: form.value.mpId,
                    name: form.value.name,
                    status: form.value.status
                }
            })
            if (res.data.status === 200) {
                ElMessage.success('修改成功')
                visible.value = false
                emit('success')
            } else {
                ElMessage.error(res.data.msg || '修改失败')
            }
        } catch (err) {
            ElMessage.error('提交失败')
        }
    })
}

defineExpose({ open })
</script>

<style scoped>
.edit-meal-schedule-dialog ::v-deep(.el-dialog__title) {
    font-size: 27.2px;
    font-weight: bold;
}

.form-body {
    padding: 10px 0;
}

.el-form-item {
    margin-bottom: 20px;
}

.dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    padding-top: 10px;
}

.el-input,
.el-select {
    width: 100%;
}
</style>
